﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EQual.MainModule.Projects.Tasks.Models
{
    public class TaskPresenter
    {
        public DataAccesLayer.EQualDataLayerFactory dataLayer;
        public TaskPresenter()
        {
            dataLayer = new DataAccesLayer.EQualDataLayerFactory();
            this.View = new EQual.MainModule.Projects.Tasks.Views.TasksGridView();
            this.View.Model = this;
            this.View.ItemAdded += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(View_ItemAdded);
        }

        void View_ItemAdded(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {
            this.AddNewTask(e.Value);
        }

        protected  IEnumerable<DataAccesLayer.Task> GetCollection()
        {
            if (ActiveProject != null)
                return this.dataLayer.GetProjectsAccesLayer().GetTasks(ActiveProject) as IEnumerable<DataAccesLayer.Task>;
            else return new WpfApplicationExperimental.DAOCollection<DataAccesLayer.Task>();
        }

        public EQual.MainModule.Projects.Tasks.Views.TasksGridView View
        {
            get;
            set;

        }

        DataAccesLayer.Task _task;
        public object ActiveItem
        {
            get { return _task; }

            set
            {
                _task = value as DataAccesLayer.Task;
               
            }
        }


        DataAccesLayer.Project _project;
        public object ActiveProject
        {
            get { return _project; }
            set
            {
                 _project = value as DataAccesLayer.Project;

                 View.Model = new { ItemsCollection = GetCollection()};
            }

        }

        internal void AddNewTask(System.Collections.IList iList)
        {
            this.dataLayer.GetProjectsAccesLayer().AddNewTaskToProject(ActiveProject, iList);
        }

        internal void AddNewTask(object p)
        {
            this.dataLayer.GetProjectsAccesLayer().AddNewTaskToProject(ActiveProject, p);
        }

    }
}
